﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.Screens.VideoPlaybackScreen
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.InputSystem;
using TaleWorlds.ScreenSystem;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.Screens
{
  public class VideoPlaybackScreen : ScreenBase, IGameStateListener
  {
    protected VideoPlaybackState _videoPlaybackState;
    protected VideoPlayerView _videoPlayerView;
    protected float _totalElapsedTimeSinceVideoStart;

    public VideoPlaybackScreen(VideoPlaybackState videoPlaybackState)
    {
      this._videoPlaybackState = videoPlaybackState;
      this._videoPlayerView = VideoPlayerView.CreateVideoPlayerView();
      this._videoPlayerView.SetRenderOrder(-10000);
    }

    protected override sealed void OnFrameTick(float dt)
    {
      this._totalElapsedTimeSinceVideoStart += dt;
      base.OnFrameTick(dt);
      if (!((NativeObject) this._videoPlayerView != (NativeObject) null) || this._videoPlaybackState == null)
        return;
      if (this._videoPlaybackState.CanUserSkip && (Input.IsKeyReleased(InputKey.Escape) || Input.IsKeyReleased(InputKey.ControllerROption)))
        this._videoPlayerView.StopVideo();
      if (this._videoPlayerView.IsVideoFinished())
      {
        this._videoPlaybackState.OnVideoFinished();
        this._videoPlayerView.SetEnable(false);
        this._videoPlayerView = (VideoPlayerView) null;
      }
      if (ScreenManager.TopScreen != this)
        return;
      this.OnVideoPlaybackTick(dt);
    }

    protected virtual void OnVideoPlaybackTick(float dt)
    {
    }

    void IGameStateListener.OnInitialize()
    {
      this._videoPlayerView.PlayVideo(this._videoPlaybackState.VideoPath, this._videoPlaybackState.AudioPath, this._videoPlaybackState.FrameRate);
      LoadingWindow.DisableGlobalLoadingWindow();
      Utilities.DisableGlobalLoadingWindow();
    }

    void IGameStateListener.OnFinalize() => this._videoPlayerView.FinalizePlayer();

    void IGameStateListener.OnActivate() => this.OnActivate();

    void IGameStateListener.OnDeactivate() => this.OnDeactivate();
  }
}
